<template>
  <div style="width: 100%; margin: 0 auto;">
    <div class="housing">住房信息</div>
    <el-row>
      <el-col class="top"
              :span="24">编号:{{pay.id}}</el-col><br />
      <el-col class="top"
              :span="24">门牌号:{{pay.houseNumber}}</el-col>
      <el-col class="top"
              :span="24">户主:{{pay.householder}}</el-col>
      <el-col class="top"
              :span="24">电话号码:{{pay.phone}}</el-col>
      <el-col class="top"
              :span="24">身份证号码:{{pay.identityCard}}</el-col>
      <el-col class="top"
              :span="24">水费:{{pay.water}}元</el-col>
      <el-col class="top"
              :span="24">电费:{{pay.power}}元</el-col>
    </el-row>
    <div class="login">
      <el-button class="logBtn"
                 @click="addWaterset()">交水费</el-button>
      <el-button class="logBtn"
                 @click="addPowerset()">交电费</el-button>

    </div>
    <el-dialog title="交水费"
               :visible="addWater"
               size="tiny"
               :modal-append-to-body="false"
               @close="closeDialog()">
      <el-form>
        <el-form-item label="编号"
                      prop="id">
          <el-input v-model="addwater.id"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="门牌号"
                      prop="houseNumber">
          <el-input v-model="addwater.houseNumber"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="户主"
                      prop="householder">
          <el-input v-model="addwater.householder"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="水费"
                      prop="water">
          <el-input v-model="addwater.water"
                    placeholder="111"></el-input>
        </el-form-item>
      </el-form>
      <el-form id="#addwater"
               ref="addwater"
               :model="addwater"
               label-width="80px">

        <el-form-item>
          <el-button type="primary"
                     @click="productAddWater()">确定</el-button>
          <el-button @click="
              addWater = false;
              canceladdwaterT();
            ">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog title="交电费"
               :visible="addPower"
               size="tiny"
               :modal-append-to-body="false"
               @close="closeDialog">
      <el-form>
        <el-form-item label="编号"
                      prop="id">
          <el-input v-model="addpower.id"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="门牌号"
                      prop="houseNumber">
          <el-input v-model="addpower.houseNumber"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="户主"
                      prop="householder">
          <el-input v-model="addpower.householder"
                    :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="电费"
                      prop="power">
          <el-input v-model="addpower.power"
                    placeholder="111"></el-input>
        </el-form-item>
      </el-form>
      <el-form id="#addpower"
               ref="addpower"
               :model="addpower"
               label-width="80px">

        <el-form-item>
          <el-button type="primary"
                     @click="productAddpower()">确定</el-button>
          <el-button @click="
              addPower = false;
              canceladdpowerT();
            ">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import axios from 'axios'
import myapi from '../api.js'
export default {
  name: 'pay',
  data() {
    return {
      message: {},
      pay: {},
      addWater: false, //当为真时，增加商品窗口显示
      addPower: false, //当为真时，商品信息修改窗口显示
      addwater: {
        id: 0,
        householder: '',
        houseNumber: '',
        water: 0,
      },
      addpower: {
        id: 0,
        householder: '',
        houseNumber: '',
        power: 0,
      },
      housing: {
        phone: '',
        houseNumber: '',
      },
    }
  },
  created() {
    console.log('接收参数成功！')
    console.log(this.$route.query.pay)
    this.pay = this.$route.query.pay
    console.log(this.pay)
    this.addpower.id = this.addwater.id = this.pay.id
    this.addpower.householder = this.addwater.householder = this.pay.householder
    this.addpower.householder = this.addwater.houseNumber = this.pay.houseNumber
    this.housing.phone = this.pay.phone
    this.housing.houseNumber = this.pay.houseNumber
  },
  methods: {
    closeDialog() {
      this.addWater = false
      this.addPower = false
    },
    canceladdwaterT() {},
    canceladdpowerT() {},
    addWaterset() {
      //新增按钮
      this.addWater = true
    },
    addPowerset() {
      this.addPower = true
    },
    productAddWater() {
      if (this.addwater.water <= 0) {
        this.$message({
          showClose: true,
          message: '交水费失败，水费不能小于等于零',
          type: 'error',
          center: true,
        })
         this.addWater = false
            this.addwater.water = 0
        return
      }
      axios
        .put(myapi.updateWater.url, {
          houseNumber: this.addwater.houseNumber,
          water: this.addwater.water,
        })
        .then((res) => {
          if (res.data != null) {
            this.$message({
              showClose: true,
              message: '交水费成功',
              type: 'success',
              center: true,
            })
            this.payAll()
            this.addWater = false
            this.addwater.water = 0
          } else if (res.data === null) {
            this.addWater = false
            this.addwater.water = 0
            this.$message({
              showClose: true,
              message: '交水费失败',
              type: 'error',
              center: true,
            })
          }
        })
    },
    productAddpower() {
      if (this.addpower.power <= 0) {
         this.addPower = false
            this.addpower.power = 0
        this.$message({
          showClose: true,
          message: '交电费失败，电费不能小于等于零',
          type: 'error',
          center: true,
        })
        return
      }
        axios
        .put(myapi.updatePower.url, {
          houseNumber: this.addwater.houseNumber,
          power: this.addpower.power,
        })
         .then((res) => {
          if (res.data != null) {
            this.$message({
              showClose: true,
              message: '交电费成功',
              type: 'success',
              center: true,
            })
            this.payAll()
            this.addPower = false
            this.addpower.power = 0
          } else if (res.data===null) {
             this.addPower = false
            this.addpower.power = 0
            this.$message({
              showClose: true,
              message: '交电费失败',
              type: 'error',
              center: true,
            })
          }
           })
    },
    payAll() {
      axios
        .post(myapi.getByIdHousing.url, {
          houseNumber: this.housing.houseNumber,
          phone: this.housing.phone,
        })
        .then((res) => {
          if (res != null) {
            this.pay = res.data
            this.addpower.id = this.addwater.id = this.pay.id
            this.addpower.householder = this.addwater.householder =
              this.pay.householder
            this.addpower.householder = this.addwater.houseNumber =
              this.pay.houseNumber
            this.housing.phone = this.pay.phone
            this.housing.houseNumber = this.pay.houseNumber
          } else {
            this.$router.push({
              path: '/inquire',
            })
          }
        })
    },
  },
}
</script>

<style>
.housing {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
}
.top {
  margin-top: 45px;
  /* padding-top:30px; */
}
</style>